import java.util.*;

public class O32 {
    public int[] levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<>();
        List<Integer> list = new ArrayList<>();
        if (root==null)
            return new int[0];
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode treeNode = queue.remove();
            list.add(treeNode.val);
            if (treeNode.left!=null)
                queue.add(treeNode.left);
            if (treeNode.right!=null)
                queue.add(treeNode.right);
        }
        int[] re = new int[list.size()];
        for (int i = 0; i < list.size(); i++)
            re[i] = list.get(i);
        return re;
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
    TreeNode(){}
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
